<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
			@close="handleClose">
			<el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
				<el-form-item v-if="formData.type==3" label="提现银行" prop="bank_id">
					<el-select class="flex-1" v-model="formData.bank_id" placeholder="请选择提现银行" clearable>
						<el-option v-for="item in bank.list" :key="item.id" :label="item.name" :value="item.id" />
					</el-select>
				</el-form-item>
				<el-form-item v-if="formData.type==3" label="姓名" prop="name">
					<el-input v-model="formData.name" clearable placeholder="请输入姓名" />
				</el-form-item>
				<el-form-item :label="formData.type==1?'地址':(formData.type==2?'账号':'卡号')" prop="account">
					<el-input v-model="formData.account" clearable placeholder="请输入账号" />
				</el-form-item>
				<el-form-item v-if="formData.type==1||formData.type==2" label="二维码" prop="img">
					<material-picker v-model="formData.img" />
				</el-form-item>
				<el-form-item label="是否禁用" prop="is_disable">
					<el-radio-group v-model="formData.is_disable" placeholder="请选择是否禁用">
						<el-radio v-for="(item, index) in dictData.system_disable" :key="index"
							:label="parseInt(item.value)">
							{{ item.name }}
						</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
		</popup>
	</div>
</template>

<script lang="ts" setup name="withdrawWalletEdit">
	import type { FormInstance } from 'element-plus'
	import Popup from '@/components/popup/index.vue'
	import feedback from '@/utils/feedback'
	import { apiWithdrawWalletEdit, apiWithdrawWalletDetail } from '@/api/withdraw_wallet'
	import { apiWithdrawBankAllByLang } from '@/api/withdraw_bank'
	import type { PropType } from 'vue'
	defineProps({
		dictData: {
			type: Object as PropType<Record<string, any[]>>,
			default: () => ({})
		}
	})
	const emit = defineEmits(['success', 'close'])
	const formRef = shallowRef<FormInstance>()
	const popupRef = shallowRef<InstanceType<typeof Popup>>()
	const mode = ref('add')


	// 弹窗标题
	const popupTitle = computed(() => {
		return mode.value == '编辑用户提现钱包'
	})

	// 数据
	const bank = reactive({
		list: []
	})

	// 表单数据
	const formData = reactive({
		id: '',
		lang_id: '',
		bank_id: '',
		type: 0,
		name: '',
		account: '',
		img: '',
		is_disable: '',
	})


	// 获取详情
	const setFormData = async (data : Record<any, any>) => {
		for (const key in formData) {
			if (data[key] != null && data[key] != undefined) {
				//@ts-ignore
				formData[key] = data[key]
			}
		}
		if (formData.type - 3 == 0) {
			bank.list = await apiWithdrawBankAllByLang({
				lang_id: formData.lang_id
			})
		}

	}

	const getDetail = async (row : Record<string, any>) => {
		const data = await apiWithdrawWalletDetail({
			id: row.id
		})
		setFormData(data)
	}

	// 提交按钮
	const handleSubmit = async () => {
		switch (formData.type) {
			case 1:
				if (!formData.account) {
					feedback.msgError('请输入地址')
					return;
				}
				// if (!formData.img) {
				// 	feedback.msgError('请上传二维码')
				// 	return;
				// }
				if (formData.account.length < 8 || formData.account.length > 128) {
					feedback.msgError('请输入正确的地址')
					return;
				}
				break;
			case 2:
				if (!formData.account) {
					feedback.msgError('请输入账号')
					return;
				}
				if (!formData.img) {
					feedback.msgError('请上传二维码')
					return;
				}
				if (formData.account.length < 6 || formData.account.length > 30) {
					feedback.msgError('请输入正确的账号')
					return;
				}
				break;
			case 3:
				if (!formData.bank_id) {
					feedback.msgError('请选择提现银行')
					return;
				}
				if (!formData.name) {
					feedback.msgError('请输入姓名')
					return;
				}
				if (!formData.account) {
					feedback.msgError('请输入卡号')
					return;
				}

				if (formData.name.length < 2 || formData.name.length > 30) {
					feedback.msgError('请输入正确的姓名')
					return;
				}
				if (formData.account.length < 6 || formData.account.length > 30) {
					feedback.msgError('请输入正确的卡号')
					return;
				}
				break;
		}
		const data = { ...formData, }
		mode.value == await apiWithdrawWalletEdit(data)
		popupRef.value?.close()
		emit('success')
	}

	//打开弹窗
	const open = (type = 'add') => {
		mode.value = type
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}



	defineExpose({
		open,
		setFormData,
		getDetail
	})
</script>